package com.open.source.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author ZonLen since on 2022/5/19 下午4:26
 */
public class DemoExecute {

  public static void main(String[] args) throws IOException {
    final File excelFile = new File(DemoExecute.class.getResource("/").getPath(), "demo.xlsx");
    excelFile.createNewFile();
    final ExcelWriter excelWriter = EasyExcel.write(excelFile)
        .withTemplate(DemoExecute.class.getResource("/").getPath()+"DemoTemplate.xlsx").build();
    try {
      WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "sheet0")
          .head(DemoData.class)
          .build();
      final ArrayList<DemoData> sheet0Data = Lists
          .newArrayList(new DemoData("sheet0", new Date(), 0.0d, "ignore")
              , new DemoData("sheet0", new Date(), 0.1d, "ignore"));
      // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
      excelWriter.write(sheet0Data, writeSheet0);

      final ArrayList<DemoSheetData> sheet1Data = Lists
          .newArrayList(new DemoSheetData("sheet1", new Date(), 1.0d, 1, "ignore")
              , new DemoSheetData("sheet1", new Date(), 1.1d, 10, "ignore"));
      WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "sheet1")
          .head(DemoSheetData.class)
          .build();
      // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
      excelWriter.write(sheet1Data, writeSheet1);
    } finally {
      // 千万别忘记finish 会帮忙关闭流
      if (excelWriter != null) {
        excelWriter.finish();
      }
    }
  }
}
